package com.fmdj.bff.driver.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.fmdj.bff.driver.controller.form.*;
import com.fmdj.bff.driver.service.DriverService;
import com.fmdj.bff.driver.service.OrderService;
import com.fmdj.common.util.CommonResult;
import com.fmdj.common.util.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Tag(name = "OrderController", description = "订单模块Web接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private DriverService driverService;

    @PostMapping("/acceptNewOrder")
    @Operation(summary = "接收新订单")
    @SaCheckLogin
    public CommonResult acceptNewOrder(@Valid @RequestBody AcceptNewOrderForm form) {
//这里可以测试手动给数据 部署时再解开
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        String result = orderService.acceptNewOrder(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);

    }

    @PostMapping("/searchDriverExecuteOrder")
    @Operation(summary = "查询司机正在执行的订单")
    @SaCheckLogin
    public CommonResult searchDriverExecuteOrderForm(@Valid @RequestBody  SearchDriverExecuteOrderForm form) {
        long driverId = StpUtil.getLoginIdAsLong(); /*因为前端会传orderId 所以只需要给driverId即可*/
        form.setDriverId(driverId);
        HashMap map = orderService.searchDriverExecuteOrder(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, map);
    }

    @PostMapping("/searchDriverCurrentOrder")
    @Operation(summary = "查询该司机当前正操作的订单")
    @SaCheckLogin
    public CommonResult searchDriverCurrentOrder() {
        SearchDriverCurrentOrderForm form = new SearchDriverCurrentOrderForm();
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        HashMap hashMap = orderService.searchDriverCurrentOrder(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT,  hashMap);
    }

    @PostMapping("/searchOrderForMoveById")
    @Operation(summary = "查询订单信息用于司乘同显功能")
    @SaCheckLogin
    public CommonResult searchOrderForMoveById(@Valid @RequestBody SearchOrderForMoveByIdForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        HashMap hashMap = orderService.searchOrderForMoveById(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }

    @PostMapping("/arriveStartPlace")
    @Operation(summary = "司机到达出发地")
    @SaCheckLogin
    public CommonResult arriveStartPlace(@Valid @RequestBody ArriveStartPlaceForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        int rows = orderService.arriveStartPlace(form);
        return CommonResult.ok().put("rows", rows);
    }

    @PostMapping("/startDriving")
    @Operation(summary = "开始代驾")
    @SaCheckLogin
    public CommonResult startDriving(@Valid @RequestBody StartDrivingForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        int rows = orderService.startDriving(form);
        return CommonResult.ok().put("rows", rows);
    }

    @PostMapping("/updateOrderStatus")
    @Operation(summary = "结束订单")
    @SaCheckLogin
    public CommonResult updateOrderStatus(@Valid @RequestBody UpdateOrderStatusForm form) {
        int rows = orderService.updateOrderStatus(form);
        return CommonResult.ok().put("rows", rows);
    }

    @PostMapping("/updateBillFee")
    @Operation(summary = "更新订单账单费用")
    @SaCheckLogin
    public CommonResult updateBillFee(@Valid @RequestBody UpdateBillFeeForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        int rows = orderService.updateOrderBill(form);
        return CommonResult.ok().put("rows", rows);
    }

    @PostMapping("/searchReviewDriverOrderBill")
    @Operation(summary = "查询司机预览账单信息")
    @SaCheckLogin
    public CommonResult searchReviewDriverOrderBill(@RequestBody @Valid SearchReviewDriverOrderBillForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        HashMap result = orderService.searchReviewDriverOrderBill(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }

    @PostMapping("/searchOrderStatus")
    @Operation(summary = "查询订单状态")
    @SaCheckLogin
    public CommonResult searchOrderStatus(@RequestBody @Valid SearchOrderStatusForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        form.setDriverId(driverId);
        int status = orderService.searchOrderStatus(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, status);
    }

    @PostMapping("/updateOrderAboutPayment")
    @Operation(summary = "司机端主动查询订单状态(如果订单状态正确什么都不用做,否则进行更新订单相关信息)")
    @SaCheckLogin
    public CommonResult updateOrderAboutPayment(@RequestBody @Valid UpdateOrderAboutPaymentForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        String result = orderService.updateOrderAboutPayment(driverId,form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
    }

    @PostMapping("/searchDriverOrderByPage")
    @Operation(summary = "分页查询司机相关联的订单列表")
    @SaCheckLogin
    public CommonResult searchDriverOrderByPage(@RequestBody @Valid SearchDriverOrderByPageForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        PageUtils pageUtils = orderService.searchDriverOrderByPage(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, pageUtils);
    }

    @PostMapping("/searchOrderById")
    @Operation(summary = "查询订单信息(查询订单信息,顾客信息以及订单评价信息)")
    @SaCheckLogin
    public CommonResult searchOrderById(@RequestBody @Valid SearchOrderByIdForm form) {
        long driverId = StpUtil.getLoginIdAsLong();
        HashMap hashMap = orderService.searchOrderById(form);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }
}
